<template />

<script lang="ts">
  import { defineComponent } from 'vue'
  import { useStore } from '@/store'
  import { param2Object } from '@/utils'
  import { useRoute, useRouter } from 'vue-router'

  export default defineComponent({
    name: 'Oauth',
    components: {},
    setup() {
      const $router = useRouter()
      const $route = useRoute()
      const $store = useStore()
      const { query } = $route
      // 正式跳转到测试
      if (query.origin && window.location.origin !== query.origin) {
        window.location.href = [query.origin, $route.fullPath].join('')
      } else if (query.code) {
        $store
          .dispatch('user/scanCodeLogin', {
            code: query.code,
            loginType: 3
          })
          .then(() => {
            if (query.redirect) {
              const redirect = String(query.redirect).split('?')
              $router.push({
                path: redirect[0],
                query: redirect[1] ? param2Object(redirect[1]) : {}
              })
            } else {
              $router.push({
                path: '/'
              })
            }
          })
          .catch(() => {
            $router.push({
              path: '/login'
            })
          })
      } else {
        $router.push({
          path: '/login'
        })
      }
    }
  })
</script>

<style></style>
